<template>
	<view class="b-bg page add-form">
		<view class="add-row">
			<view class="ad-label">
				<text class="icon-warning">*</text>
				<text>姓名</text>
			</view>
			<view class="ad-inputBox">
				<uni-easyinput class="uni-mt-5" clearSize='0'  trim="all" v-model="form_data.student_name" placeholder="请输入内容" ></uni-easyinput>
			</view>
		</view>
		<view class="add-row">
			<view class="ad-label">
				<text class="icon-warning">*</text>
				<text>地区</text>
			</view>
			<view class="ad-inputBox">
				<uni-data-select
					  v-model="form_data.city_id"
					  :localdata="rangeCity"
					  :clear="false"
					  :disabled="true"
					  @change="changeCity"
					></uni-data-select>
			</view>
		</view>
		<view class="add-row">
			<view class="ad-label">
				<text class="icon-warning">*</text>
				<text>区县</text>
			</view>
			<view class="ad-inputBox">
				<uni-data-select
				  v-model="form_data.county_id"
				  :localdata="rangeCounty"
				  :clear="false"
				  :disabled="disableCounty"
				   @change="changeCounty"
				></uni-data-select>
			</view>
		</view>
		<view class="add-row">
			<view class="ad-label">
				<text class="icon-warning">*</text>
				<text>学校</text>
			</view>
			<view class="ad-inputBox">
				<uni-data-select
				  v-model="form_data.school_id"
				  :localdata="rangeSchool"
				  :clear="false"
				></uni-data-select>
			</view>
		</view>
		<view class="add-row">
			<view class="ad-label">
				<text class="icon-warning">*</text>
				<text>班级</text>
			</view>
			<view class="ad-inputBox">
				<uni-easyinput class="uni-mt-5" clearSize='0'  trim="all" v-model="form_data.class_name" placeholder="请输入内容" ></uni-easyinput>
			</view>
		</view>
		<view class="add-sbimtbox">
			<view class="bt bt-a" @tap="submit">完成</view>
		</view>
	</view>
</template>

<script>
	import mixin from '@/util/mixin.js';
	import baseUrl from '@/util/path.js';
	export default {
		mixins:[mixin],
		data() {
			return {
				form_data:{
					city_id:"",
					county_id:'',
					school_id:"",
					student_name:'',
					class_name:""
				},
				disableCounty:false,
				rangeCity:[],
				rangeCounty:[],
				rangeSchool:[]
			}
		},
		onLoad() {
			console.log(baseUrl);
			this.getAra();
			const v = baseUrl.v;
			if(v == 'v7') {
				this.form_data.city_id = 420500;
				this.changeCity(420500)
				this.form_data.county_id = 420528;
				this.disableCounty = true;
				this.changeCounty(420528);
			}
			if (v == 'v5') {
				this.form_data.city_id = 421100;
				this.changeCity(421100)
			}
		
		},
		methods: {
			async getAra() {
				const res = await this.$http({
					  url:'api/public/region',
					  method:'post',
					  data:{}
				},'lb')
				// console.log(res);
				this.rangeCity = res.map((item)=> {
					return {
						value:item.id,
						text:item.name
					}
				})
				
				this.rangeCity.unshift({
					text:'请选择',
					value:""
				})
				
			},
			changeCity(e){
				//console.log(e);
				this.rangeSchool = [{text:'请先选择区县',value:""}];
				this.form_data.school_id = "";
				this.form_data.county_id = "";
				this.getCount(e);
			},
			async getCount(e) {
				this.form_data.school_id = "";
				const res = await this.$http({
					  url:'api/public/region',
					  method:'post',
					  data:{
						  pid:e
					  }
				},'lb')
				// console.log(res);
				this.rangeCounty = res.map((item)=> {
					return {
						value:item.id,
						text:item.name
					}
				})
				
				this.rangeCounty.unshift({
					text:'请选择',
					value:""
				})
			},
			
			async changeCounty(e) {
				const res = await this.$http({
					  url:'api/public/get_all_school',
					  method:'post',
					  data:{
						  county_id:e
					  }
				},'lb')
				this.rangeSchool = res.map((item)=> {
					return {
						value:item.id,
						text:item.name
					}
				})
				this.rangeSchool.unshift({
					text:'请选择',
					value:""
				})
			},
			async submit() {
				if(!this.form_data.student_name) {
					this.toast('请输入姓名')
					return
				}
				if(!this.form_data.city_id) {
					this.toast('请选择地区')
					return
				}
				if(!this.form_data.county_id) {
					this.toast('请选择区县')
					return
				}
				if(!this.form_data.school_id) {
					this.toast('请选择学校')
					return
				}
				
				const res = await this.$http({
				  url:'api/student/add_student',
				  method:'post',
				  data:this.form_data
			     },'lb')
				 
				 
			    if(res) {
					this.toast("操作成功");
					this.form_data = {
						city_id:"",
						county_id:"",
						school_id:"",
						student_name:'',
						class_name:""
					}
					this.rangeCounty = [];
					this.rangeSchool = [];
					uni.navigateBack({
						delta: 1
					});
				}
			}
		}
	}
</script>

<style lang="scss">
	 .add-sbimtbox {
		 padding-top: 55rpx;
		 @include bt-s-1();
	 }
	 
     .add-row {
		 background-color: #fff;
		 display: flex;
		 border-radius: 16rpx;
		 margin-bottom: 16px;
		 .ad-label {
			 width: 120rpx;
			 height: 100rpx;
			 font-size: 28rpx;
			 @include center();
		 }
		 .ad-inputBox {
			 width: 544rpx;
			 height: 100rpx;
			 
			 @include center();
		 }
	 }
</style>
